
function News(idx, hl, d, s, t, p, he, af, sds) {
  this.id = idx;
  this.headline = hl;
  this.createdDate = d;
  this.pin = p;
  this.source = s;
  this.tooltip = t;
  this.heb = he;
  this.afl = af;
  this.sd = sds;
}
var minremain = 4;
var nclip = 12;
var stop_retrieve = false;
var step_height = 0;
var movedsteps = 0;
var locked = false;
var nofnews = 0;
var listNews = new Array();
var currentPos = 1;
var fullListSize = 0;
var marketid = 1;
var companyid = 0;
var industryid = 0;
var type = 1;

var req;
var which;
var returnLocation;

function retrieveURL(url, position) {
      var retrieveReq = false;
      returnLocation = position;
      retrieveReq = getXMLHTTPRequest();
      if (retrieveReq) {
            retrieveReq.open("POST", url, true);
            retrieveReq.onreadystatechange = function () {
                  processStateChange(retrieveReq);
            };
            retrieveReq.send(null);
      }
      else{
            //alert('Cannot init XML HTTP Request');
      }
}

function processStateChange(retrieveReq) {
  if (retrieveReq.readyState == 1) {
    //document.getElementById(returnLocation).innerHTML = "";
  }

  if (retrieveReq.readyState == 4) { // Complete
    if (retrieveReq.status == 200) { // OK response
      //parse xml and store them into array
      parseXML(getXML(retrieveReq));

      //display them
      displayScrollNews();
    }
    else {
      //alert("Problem: " + req.statusText);
    }
  }
}

function displayScrollNews(){
      if (type == 2) {//otc
        displayListNews(listNews, returnLocation);
      } else {
        displayListNewsWithPin(listNews, returnLocation);
      }
}


function parseXML(xmlDomForNews) {
  listNews = new Array();

  //get list news
  var list = selectNodeList(xmlDomForNews, "/news/new");
  for (var i=0; i<list.length; i++) {
     listNews.push(new News(list[i].getAttribute("id"), list[i].getAttribute("headline"), list[i].getAttribute("createdDate"), list[i].getAttribute("sourceNews"), list[i].getAttribute("tooltip"), list[i].getAttribute("pin"),list[i].getAttribute("heb"),list[i].getAttribute("afl"),list[i].getAttribute("sd")));
  }


  //get position node
  var listPositions = selectNodeList(xmlDomForNews, "/news/position/position-node");
  if (listPositions.length > 0) {
      currentPos = parseInt(listPositions[0].firstChild.nodeValue);
  }

  //get list size node
  var listSizeNodes = selectNodeList(xmlDomForNews, "/news/list-size/size-node");
  if (listSizeNodes.length > 0) {
    fullListSize = parseInt(listSizeNodes[0].firstChild.nodeValue);
  }

  //get market node
  var listMarketNodes = selectNodeList(xmlDomForNews, "/news/market/id-node");
  if (listMarketNodes.length > 0) {
    if(listMarketNodes[0].firstChild) {
	  marketid = listMarketNodes[0].firstChild.nodeValue;
	}
  }

  //get company node
  var listCompanyNodes = selectNodeList(xmlDomForNews, "/news/company/id-node");
  if (listCompanyNodes.length > 0) {
	if(listCompanyNodes[0].firstChild) {
      companyid = listCompanyNodes[0].firstChild.nodeValue;
	}
  }

  //get industry node
  var listIndustryNodes = selectNodeList(xmlDomForNews, "/news/industry/id-node");
  if (listIndustryNodes.length > 0) {
    if(listIndustryNodes[0].firstChild) {
  	  industryid = listIndustryNodes[0].firstChild.nodeValue;
	}
  }

  //get sector node
  var listSectorNodes = selectNodeList(xmlDomForNews, "/news/sector/id-node");
  if (listSectorNodes.length > 0) {
    if(listSectorNodes[0].firstChild) {
      //we use industryid for sector's variant. So, we needn't change function move
  	  industryid = listSectorNodes[0].firstChild.nodeValue;
	}
  }

  //get type node
  var listTypeNodes = selectNodeList(xmlDomForNews, "/news/type/type-node");
  if (listTypeNodes.length > 0) {
    type = parseInt(listTypeNodes[0].firstChild.nodeValue);
  }

  //get numOfNoPin node
  var listnumOfNoPinNodes = selectNodeList(xmlDomForNews, "/news/numOfNoPin/numOfNoPin-node");
  if (listnumOfNoPinNodes.length > 0) {
    numOfNoPin = parseInt(listnumOfNoPinNodes[0].firstChild.nodeValue);
  }
}


function displayListNewsWithPin(items, position) {

	var main = getElem(position);
	var scrollnewslayer;
	if(document.getElementById('scrollnewslayer')){
		var mainNode = document.getElementById('ajaxnews');
		var divRemove = document.getElementById('scrollnewslayer');
		mainNode.removeChild(divRemove);
	}

		scrollnewslayer = document.createElement("div");
		scrollnewslayer.setAttribute('id','scrollnewslayer');
		scrollnewslayer.setAttribute('style','position: relative');
		scrollnewslayer.setAttribute('className','table_read_news');
		scrollnewslayer.style.cssText = 'position:relative; top:0px;';



	main.appendChild(scrollnewslayer);

	for (var i=0; i<items.length; i++) {
    var node = document.createElement("div");
	node.setAttribute('id','div_' + items[i].id);
    var html = "<table id='tbn_" + items[i].id + "' width='100%' cellpadding='0' cellspacing='0' border='0' height='25'  class='table_read_news'>";
    html += "<tr>";
    if(items[i].pin=="no_pin"){
    html += "<td width='7%' height='17px' valign='top'>" + "<a border='0' href='javascript:sendNewId(" + items[i].id + ")'></a>" + "</td>";
    }else{
    html += "<td id='tdn_" + items[i].id + "'  style='font-weight: normal' width='7%' height='17px' valign='top'>" + "<a border='0' href='javascript:sendNewId(" + items[i].id + ")'><img align='top' src='" + basePath + "/stylesheets/images/pins/" + items[i].pin + ".gif' width='17px' height='17px' border='0'></a>" + "</td>";
    }
    /*
    if(items[i].heb == "true"){
    	html += "<td height='17px' style='color:#18479B;font-size:12px;'><a href='" + basePath + "/news/detail.do?id=" + items[i].id + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a></td>";
    }else if (items[i].afl != ""){

    	html += "<td height='17px' style='color:#18479B;font-size:12px;'><a href='" + items[i].afl + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a></td>";
    }*/

    if (((null != items[i].afl) && (items[i].afl != "")) && (items[i].heb == 'false') && ((null == items[i].sd) || (items[i].sd == ""))) {
    	html += "<td height='17px' align='justify'>" + items[i].createdDate + (items[i].createdDate == "" ? "" : " - ")
    		+ " <a href='" + basePath + "/downloadFile?fileid=" + items[i].id + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a> "
    	 	+ (items[i].source == "" ? "" : "(") + items[i].source +  (items[i].source == "" ? "" : ")") + "</td>";
    }else{
		html += "<td height='17px' align='justify'>" + items[i].createdDate + (items[i].createdDate == "" ? "" : " - ")
    		+ " <a href='" + basePath + "/news/detail.do?id=" + items[i].id + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a> "
    	 	+ (items[i].source == "" ? "" : "(") + items[i].source +  (items[i].source == "" ? "" : ")") + "</td>";
    }

    html += "</tr>";
    /*
    html += "<tr>";
    html += "<td width='7%'></td>";
    html += "<td style='color:#555;font-size:10px;font-family:Tahoma;'>" + items[i].source + (items[i].source == "" ? "" : " - ") + items[i].createdDate + "</td>";
    html += "</tr>";
    html += "<tr><td colspan='2' height='2px'></td></tr>";
    */
    html += "</table>";
    node.innerHTML =  html;
    scrollnewslayer.appendChild(node);
  }
}

function displayListNews(items, position) {
  var main = getElem(position);
  main.innerHTML = "";
	var scrollnewslayer;
	if(document.getElementById('scrollnewslayer')){
		scrollnewslayer = document.getElementById('scrollnewslayer');
	}else {
		scrollnewslayer = document.createElement("div");
		scrollnewslayer.setAttribute('id','scrollnewslayer');
		main.appendChild(scrollnewslayer);
	}
    for (var i=0; i<items.length; i++) {
    var node = document.createElement("div");
    var html = "<table width='350' cellpadding='0' cellspacing='0' border=0 style='margin-top:0px;padding-top:0px;text-align: left;'>"
    html += "<tr>";
    html += "<td height='17px' style='color:#18479B;font-size:12px;'><a href='" + basePath + "/news/detail.do?id=" + items[i].id + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a></td>";
    html += "</tr>";
    html += "<tr>";
    html += "<td style='color:#555;font-size:10px;font-family:Tahoma;'>" + items[i].source + (items[i].source == "" ? "" : " - ") + items[i].createdDate + "</td>";
    html += "</tr>";
    html += "<tr><td height='2px'></td></tr>";
    html += "</table>";
    node.innerHTML =  html;
    scrollnewslayer.appendChild(node);
  }
}

function goLoadIndustryNews(indid, typeNews) {
  retrieveURL("ajaxscrollnews.do?start=1&industryCode="+indid+"&type="+typeNews, "ajaxnews");
}

function goLoadCompanyNews(compid, typeNews) {
  retrieveURL("ajaxscrollnews.do?start=1&symbol="+compid+"&type="+typeNews, "ajaxnews");
}

function goLoadOTCNews(marketid, typeNews) {
  retrieveURL("ajaxscrollnews.do?start=1&symbol="+marketid+"&type="+typeNews, "ajaxnews");
}

function goLoadNews(marketid, typeNews) {
  retrieveURL("ajaxscrollnews.do?start=1&symbol="+marketid+"&type="+typeNews, "ajaxnews");
}

function getElem(div) {
  return document.getElementById ? document.getElementById(div) : document.all[div]
}

/*function movenewslayer(step){
	movedsteps += step;
	if(step_height == 0){
		if(document.getElementById('scrollnewslayer') && document.getElementById('scrollnewslayer').firstChild &&
			document.getElementById('scrollnewslayer').firstChild.offsetHeight) {
			step_height =  document.getElementById('scrollnewslayer').firstChild.offsetHeight;
		}
	}
	document.getElementById('scrollnewslayer').style.top = movedsteps*step_height + "px";
}*/

function movenewslayer(step){
	movedsteps += step;
	var url = basePath + "/ajaxscrollnews.do?start="+movedsteps+"&symbol="+symbol+"&type="+type;
	retrieveNewsXML2(url,"ajaxnews");

	//document.getElementById('scrollnewslayer').style.top = movedsteps*step_height + "px";
}

function move(step) {
	//step = -step; //28092009 Vudinh
	if(step == 0){
		//return to top
		document.getElementById('scrollnewslayer').style.top = "0px";
		movedsteps = 0;
	}
	else{
		movenewslayer(step);
	}
}

/*function movePage(step) {
	if(nofnews<=itemPerPage)
		return ;
	if(currentPage==0 && step==-1)
		return ;

	if(currentPage==totalPages  && step==1)
		return ;
	move((step)*itemPerPage);
	currentPage = currentPage + step;
}*/
function movePage(step) {
	var curPage = currentPage + step;
	if(curPage < 0){
		return;
	}
	move((step)*itemPerPage);
	currentPage = currentPage + step;
}
function retrieveNewsXML(symbol, type, container){
	//duclh
	returnLocation = container;
	var url = "ajaxscrollnews.do?start=1&symbol=" + symbol + "&type=" + type;
	/*
	new Ajax.Request(url,{ method: 'get', onSuccess: function(transport)
			{
				parseXML(transport.responseXML);
				displayScrollNews();
				nofnews = listNews.length;

			}
		});
	*/
}
function retrieveNewsXML2(url, container){
	returnLocation = container;
	jQuery.ajax({
		type: "GET",
		url: url,
		success: function(xml){
			parseXML(xml);
			displayScrollNews();
			nofnews = listNews.length;
			totalPages = Math.floor(nofnews/itemPerPage);
			document.getElementById('scrollnewslayer').className = 'table_read_news2';
		}
	});
}


function viewNewsByDate(date) {
  retrieveURL("ajaxscrollnews.do?start=-1&date="+date+"&symbol="+companyid+"&type="+type, "ajaxnews");
}

function viewNewsByID(newid, typeNews, cid) {

  if(typeNews == 1) {
      retrieveURL("ajaxscrollnews.do?start=-2&nid="+newid+"&marketCode="+cid+"&type="+typeNews, "ajaxnews");
  } else if(typeNews == 3) {
      retrieveURL("ajaxscrollnews.do?start=-2&nid="+newid+"&symbol="+cid+"&type="+typeNews, "ajaxnews");
  }
  else if(typeNews == 5) { //sector
      retrieveURL("ajaxscrollnews.do?start=-2&nid="+newid+"&industryCode="+cid+"&type="+typeNews, "ajaxnews");
  }
  else {
      retrieveURL("ajaxscrollnews.do?start=-2&nid="+newid+"&industryCode="+cid+"&type="+typeNews, "ajaxnews");
  }
}

//Sector
function goLoadSectorNews(indid, typeNews) {
	retrieveURL("ajaxscrollnews.do?start=1&industryCode="+indid+"&type="+typeNews, "ajaxnews");
}

function parseNewsXML(xmlDomForNews) {
  //duclh
  //get list news
  var list = selectNodeList(xmlDomForNews, "/news/new");
  for (var i=0; i<list.length; i++) {
     listNews.push(new News(list[i].getAttribute("id")
			, list[i].getAttribute("headline")
			, list[i].getAttribute("createdDate")
			, list[i].getAttribute("sourceNews")
			, list[i].getAttribute("tooltip")
			, list[i].getAttribute("pin")));
  }
}
function appendScrollNewsLayer(frompos,container){
	//duclh
	var main = $(container);
	var scrollnewslayer;
	if(document.getElementById('scrollnewslayer')){
		scrollnewslayer = document.getElementById('scrollnewslayer');
	}else {
		scrollnewslayer = document.createElement("div");
		scrollnewslayer.setAttribute('id','scrollnewslayer');
		main.appendChild(scrollnewslayer);
	}
	var items = listNews;

	for (var i=frompos; i<items.length; i++) {
	    var node = document.createElement("div");
	    var html = "<table height='45' width='350' cellpadding='0' cellspacing='0' border='0' style='margin-top:0px;padding-top:0px;text-align: left;'>";
	    html += "<tr>";
	    if (items[i].pin == "no_pin"){
	    	numOfNoPin++;
	    }

	    if(items[i].pin=="no_pin"){
    		html += "<td width='7%' height='17px' valign='top'>" + "<a border='0' href='javascript:sendNewId(" + items[i].id + ")'></a>" + "</td>";
    	}else{
    		html += "<td width='7%' height='17px' valign='top'>" + "<a border='0' href='javascript:sendNewId(" + items[i].id + ")'><img align='top' src='" + basePath + "/stylesheets/images/pins/" + items[i].pin + ".gif' width='17px' height='17px' border='0'></a>" + "</td>";
    	}

	    //html += "<td width='7%' valign='top' height='19px'><a border='0' href='javascript:sendNewId(" + items[i].id + ")'><img align='top' src='./images/pins/" + items[i].pin + ".gif'  width='19px' height='19px' border='0px'></a></td>";

	    html += "<td height='17px' style='color:#18479B;font-size:12px;'><a href='" + basePath + "/news/detail.do?id=" + items[i].id + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a></td>";
	    html += "</tr><tr><td width='7%' ></td><td style='color:#555;font-size:10px;font-family:Tahoma;'>";
	    html += items[i].source + (items[i].source == "" ? "" : " - ") + items[i].createdDate;
	    html += "</td></tr><tr><td colspan='2' height='2px'></td></tr></table>";

	    node.innerHTML =  html;
	    scrollnewslayer.appendChild(node);
	}

}
function appendScrollNewsLayerForOTC(frompos,container){
	//anhhc
	var main = $(container);
	var scrollnewslayer;
	if(document.getElementById('scrollnewslayer')){
		scrollnewslayer = document.getElementById('scrollnewslayer');
	}else {
		scrollnewslayer = document.createElement("div");
		scrollnewslayer.setAttribute('id','scrollnewslayer');
		main.appendChild(scrollnewslayer);
	}
	var items = listNews;
	for (var i=frompos; i<items.length; i++) {
	    var node = document.createElement("div");
	    var html = "<table width='350' cellpadding='0' cellspacing='0' border=0 style='margin-top:0px;padding-top:0px;text-align: left;'>"
	    html += "<tr>";
	    html += "<td height='17px' style='color:#18479B;font-size:12px;'><a href='" + basePath + "/news/detail.do?id=" + items[i].id + "' title='" + items[i].tooltip + "'>" + items[i].headline + "</a></td>";
	    html += "</tr>";
	    html += "<tr>";
	    html += "<td style='color:#555;font-size:10px;font-family:Tahoma;'>" + items[i].source + (items[i].source == "" ? "" : " - ") + items[i].createdDate + "</td>";
	    html += "</tr>";
	    html += "<tr><td height='2px'></td></tr>";
	    html += "</table>";

	    node.innerHTML =  html;
	    scrollnewslayer.appendChild(node);
	}

}

//disable this function if use new flash chart
//function sendNewId(id){
//		flashProxy.call('selectFlag', id);
//}
